package ThreadPool;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPoolTest2 {
    public static void main(String[] args) throws  Exception {
        //1,通过ThreadExecutor创建一个线程池对象【开发中推荐】
//        ExecutorService pool =
//                new ThreadPoolExecutor(3,5,8,
//                        TimeUnit.SECONDS,new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(),
//                        new ThreadPoolExecutor.CallerRunsPolicy());

        //固定3个线程，可以复用,【存在安全问题，不推荐】
        ExecutorService pool = Executors.newFixedThreadPool(3);
        //2.使用线程池处理Callable任务
        Future<String> f1 = pool.submit( new MyCallable(100));
        Future<String> f2 = pool.submit( new MyCallable(200));
        Future<String> f3 = pool.submit( new MyCallable(300));
        Future<String> f4 = pool.submit( new MyCallable(400));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
